<template>
  <v-dialog
    :title="title"
    :close-on-click-modal="false"
    v-model="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'" label-width="120px">
      <el-row  >
      
        <el-col :span="24">
            <el-form-item label="收票时间" prop="receiptTime"
                :rules="[{required: true, message:'收票时间不能为空', trigger:'blur'}
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.receiptTime"
                      type="datetime"
                      value-format="YYYY-MM-DD HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
            <el-col :span="24">
             <el-form-item label="发票类型" prop="receiptType" :rules="[{required: true, message:'发票类型不能为空', trigger:'blur'}
                 ]">
            <el-select v-model="inputForm.receiptType" placeholder="请选择" style="width: 100%;">
              <el-option v-for="item in $dictUtils.getDictList('ticket_type')" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item></el-col>
        <el-col :span="24">
            <el-form-item label="收票金额" prop="receiptAmount"
                :rules="[{required: true, message:'收票金额不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.receiptAmount" placeholder="请填写收票金额"     ></el-input>
           </el-form-item>
        </el-col>

        <el-col :span="24">
            <el-form-item label="备注" prop="remark"
                :rules="[
                 ]">
              <el-input v-model="inputForm.remark" placeholder="请填写备注"  type="textarea" :rows="6"   ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <template #footer>    
        <span class="dialog-footer">
          <el-button @click="visible = false" icon="circle-close">关闭</el-button>
          <el-button  type="primary" v-if="method != 'view'" @click="doSubmit()" icon="circle-check" v-noMoreClick>确定</el-button>
        </span>
    </template>    
  </v-dialog>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  import purchaseReceiptService from '@/api/receipt/purchaseReceiptService'
  import tPurchaseContractService from '@/api/contract/tPurchaseContractService'   
  import projectMainService from '@/api/project/projectMainService'   
  import purchaseService from '@/api/purchase/purchaseService'   
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        tPurchaseContractService: tPurchaseContractService,  
        projectMainService: projectMainService,  
        purchaseService: purchaseService,  
        inputForm: {
          id: '',
          contract: {
            id: ''
          },
          project: {
            id: ''
          },
          purchase: {
            id: ''
          },
          receiptTime: '',
          receiptAmount: '',
          remark: '',
          receiptType:""
        }
      }
    },
    components: {
      GridSelect
    },
    created () {
    },
    methods: {
      init (method, id) {
        this.method = method
       
        if (method === 'add') {
         this.inputForm.id = ''
          this.inputForm.contract.id = id.contract.id;
          this.inputForm.purchase.id = id.purchase.id;
          this.inputForm.project.id = id.project.id;
          this.title = `新建采购收票`
        } else if (method === 'edit') {
          this.inputForm.id = id
          this.title = '修改采购收票'
        } else if (method === 'view') {
          this.inputForm.id = id
          this.title = '查看采购收票'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            purchaseReceiptService.queryById(this.inputForm.id).then((data) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            purchaseReceiptService.save(this.inputForm).then((data) => {
              this.visible = false
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>

  
